package com.atrix.CinemaView.controller;

import com.atrix.CinemaView.pojo.dto.info.MdInfo;
import com.atrix.CinemaView.pojo.dto.MdStreamData;
import com.atrix.CinemaView.pojo.vo.R;
import com.atrix.CinemaView.service.MdService;
import com.atrix.CinemaView.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/api/md")
@Slf4j
@Api("文档控制器")
public class MdController {
    @Autowired
    private MdService mdService;
    
    @GetMapping("/info")
    @ApiOperation("获取所有文档")
    public R<List<MdInfo>> getMdInfo() {
        return R.<List<MdInfo>>success().setData(mdService.getInfo());
    }
    
    @GetMapping
    @ApiOperation("随机获取文档")
    public void getRandomMd(HttpServletResponse response) {
        MdStreamData md = mdService.getRandomMd();
        response.setContentType("text/plan");
        FileUtil.outputFile(md, response);
    }
    
    @GetMapping("/{index}")
    @ApiOperation("通过文档容器获取随机文档")
    public void getRandomMdInContainer(@ApiParam("容器索引") @PathVariable("index") Integer index,
                                       HttpServletResponse response) {
        MdStreamData md = mdService.getRandomMdInContainer(index);
        response.setContentType("text/plan");
        FileUtil.outputFile(md, response);
    }
    
    @GetMapping("/{containerIndex}/{index}")
    @ApiOperation("根据容器索引和文档索引获取文档")
    public void getMd(@ApiParam("文档容器索引") @PathVariable("containerIndex") Integer containerIndex,
                      @ApiParam("容器内文档索引") @PathVariable("index") Integer index,
                      HttpServletResponse response) {
        MdStreamData md = mdService.getMd(containerIndex, index);
        response.setContentType("text/plan");
        FileUtil.outputFile(md, response);
    }
}
